import 'package:flutter/material.dart';
import 'package:freemusic/components/WidgetTitle.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/utils/Util.dart';

class SearchHistoryWidget extends StatelessWidget {
  final String keyword;
  final List<String> keywords;

  SearchHistoryWidget({this.keyword, this.keywords});

  List<HistoryWord> _renderHistoryWords() {
    List<HistoryWord> widgets = List();
    List<String> keywords = this.keywords;
    Color wordColor = Util.themeColor;
    keywords.forEach((item) {
      HistoryWord historyWord;
      if(item ==keyword){
        historyWord = HistoryWord(item, wordColor: wordColor,);
      }else{
        historyWord = HistoryWord(item,);
      }
      widgets.add(historyWord);
    });
    return widgets;
  }

  void _handleOnRemove(BuildContext context){
    Color wordColor = Util.themeColor;
    showDialog(
      context: context,
      builder: (ctx)=> AlertDialog(
        title: Text("提示"),
        content: Text("是否要删除所有搜索记录？"),
        actions: <Widget>[
              FlatButton(
                child: Text(
                  "取消",
                  style: TextStyle(color: wordColor),
                ),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              FlatButton(
                child: Text(
                  "确认",
                  style: TextStyle(color: wordColor),
                ),
                onPressed: () {
                  SongServices.getInstace().deleteAllKerwordHistory();
                  Navigator.pop(context);
                },
              ),
            ],
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          WidgetTitle(
            "搜索历史",
            icon: Icon(Icons.delete_forever),
            onTap: (){this._handleOnRemove(context);},
          ),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: this._renderHistoryWords(),
            ),
          ),
        ],
      ),
    );
  }
}

class HistoryWord extends StatelessWidget {
  final String keyword;
  final Color wordColor;

  HistoryWord(this.keyword, {this.wordColor});

  void handleOnTap(){
    SongServices.getInstace().search(keyword);
  }

  @override
  Widget build(BuildContext context) {
    String keyword = this.keyword;
    return GestureDetector(
      onTap: this.handleOnTap,
      child: Container(
        padding: EdgeInsets.only(top: 8, bottom: 8, left: 16, right: 16),
        margin: EdgeInsets.all(4),
        decoration: BoxDecoration(
          color: Color.fromRGBO(220, 220, 220, 1),
          borderRadius: BorderRadius.all(
            Radius.circular(18),
          ),
        ),
        child: Text(
          keyword,
          textAlign: TextAlign.center,
          style: TextStyle(color: wordColor),
        ),
      ),
    );
  }
}
